summaryrefslogtreecommitdiff
blob: 022c334565c3ad31654c4c870303e43d6713d26f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/python
import getopt, sys, os, string, urllib, re
from filetricks import *
from ebuild import *

#note: this script makes use of exceptions

__doc__="Usage: "+sys.argv[0]+" <local repository directory> <action> [<action arguments>...]"

REPO_MYDIR=".g-cran"
REPO_CONFFILE="settings.cfg"

#Parse package options into values we can work with in accordance with PMS
def pmsify_packages_data(data):
    #fix settings:
    if 'package' not in data:
    	data['package']='test'
    if not re.match('[a-zA-Z0-9+_].*',data['package']): #package name may not be valid according to PMS
        data['package']='_'+package_name
    if re.match('.*-[0-9]+',data['package']):
        data['package']=package_name+'_'
    #data['package']=data['package']
    if 'version' not in data: #set a version even if we have none
         data['version']='0'
    else:
        data['version']=data['version'].replace('-','.') #some CRAN-style versions have the form 0.1-1, which we transform into 0.1.1
    if 'depends' in data:
        #dependencies=re.split('([a-zA-Z0-9+_-]+[ \t]+\(.+?\)[ \t]+)',data['depends'])
        dependencies=re.split(r'([a-zA-Z0-9+_-]+[ \t]+\\(.+?\\))|([a-zA-Z0-9+_-]+)[ \t]+',data['depends']) #split into packages + versions
        data['depends']=['dev-lang/R',]+dependencies #fixme check
    else: #some packages don't set dependencies
        data['depends']=['dev-lang/R',]
    return data

def read_packages(package_filename):
    packages_file=open(package_filename,"r")
    file_parts=EmptyLinesFile(packages_file) #this is where we split the PACKAGES file into several file parts
    packages=[]
    import rfc822
    while not file_parts.eof:
        new_package=dict(rfc822.Message(file_parts).items()) #read part of PACKAGES file
        pmsify_packages_data(new_package) #fix values
        packages.append(new_package) #store in dict
    print packages

def action_sync(repo_location,remote_uri):
    if not os.path.isdir(os.path.join(repo_location, REPO_MYDIR)):
        if os.path.isdir(repo_location):
            os.mkdir(os.path.join(repo_location,REPO_MYDIR))
        else:
            raise IOError
    packages_filename=os.path.join(repo_location, REPO_MYDIR, 'PACKAGES')
    urllib.urlretrieve(remote_uri+'/src/contrib/PACKAGES',packages_filename)
    read_packages(packages_filename)
    raise NotImplementedError

def list_categories(repo_location):
    print "dev-R"

def list_packages(repo_location):
    package_file=read_packages(os.path.join(repo_location,REPO_MYDIR,'PACKAGES'))
    for package, package_metadata in package_file.items():
        print "dev-R/"+package+"-"+package_metadata['version']

def usage():
    print __doc__

#available_actions={'usage':action_usage}
pkg_funcs=['pkg_pretend','pkg_setup','src_unpack','src_prepare','src_configure','src_compile',
    'src_test','src_install','pkg_preinst','pkg_postinst','pkg_prerm','pkg_postrm','pkg_config','pkg_info','pkg_nofetch']
actions_wanted=['usage','sync','list-categories','list-packages','package']+pkg_funcs

def main():
    arguments=sys.argv[1:]
    #print options, arguments
    if len(arguments)<2: #we need at least a local repository location and an action
        usage()
        sys.exit(0)
    action=arguments[1]
    repo_location=os.path.abspath(arguments[0])
    if action=='sync':
        if len(arguments)<2:
            print "The 'sync' action takes the following parameters:"
            print " * remote_repository_uri"
        if len(arguments)==3:
            remote_repo=arguments[2]
        #else:
        #    settings=read_settings(repo_location)
        #    remote_repo=settings['remote_repository_uri']
        action_sync(repo_location,remote_repo)
    elif action=='list-categories':
        list_categories(repo_location)
    elif action=='list-packages':
        list_packages(repo_location)
    elif action=='usage':
        usage()
    elif action in actions_wanted:
        raise NotImplementedError
    else:
        usage()

if __name__ == "__main__":
    main()