summaryrefslogtreecommitdiff
blob: 68981095f53d8e6915ef60d19b6f3c9dabc708f8 (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
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/python
import getopt, sys, os, string, urllib, re
from ebuild import *
import phases
from cran_read import *
from settings import *

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

#sync a local repository's PACKAGES file
def action_sync(repo_location,remote_uri):
    if not os.path.isdir(os.path.join(repo_location, REPO_MYDIR)):
        os.mkdir(os.path.join(repo_location,REPO_MYDIR))
    packages_filename=os.path.join(repo_location, REPO_MYDIR, 'PACKAGES')
    packages_rds_filename=os.path.join(repo_location, REPO_MYDIR, 'packages.rds')
    try:
        #we first try to get a serialized full database... this works for CRAN
        urllib.urlretrieve(remote_uri+'/web/packages/packages.rds',packages_rds_filename)
        R_script=os.path.join(os.path.dirname(__file__),'convert_packages_rds.R')
        returncode=os.system('R --quiet --file='+R_script+' --args '+packages_rds_filename+' '+packages_filename)
        if returncode:
            raise RuntimeError('Could not convert packages.rds')
    except:
        urllib.urlretrieve(remote_uri+'/src/contrib/PACKAGES',packages_filename)
    repo_file=open(os.path.join(repo_location,REPO_MYDIR,'remote_uri'),'w')
    repo_file.write(remote_uri)

#list categories in this repository
def list_categories(repo_location):
    print "dev-R"

#idem ditto
def list_packages(repo_location):
    packages=read_packages(os.path.join(repo_location,REPO_MYDIR,'PACKAGES'),repo_location)
    for package in packages:
        print 'dev-R/'+package.ebuild_vars['pn'],package.ebuild_vars['pv']

#generate a tree of ebuilds... note that we only link ebuild files
#metadata.xml and Manifest and whatnot is not generated
def generate_tree(repo_location):
    packages=read_packages(os.path.join(repo_location,REPO_MYDIR,'PACKAGES'),repo_location)
    ebuild_file=os.path.join(os.path.dirname(__file__),'cran.ebuild')
    for package in packages:
        ebuild_dir=os.path.join(repo_location,'dev-R',package.ebuild_vars['pn'])
        if not os.path.exists(ebuild_dir):
            os.makedirs(ebuild_dir)
        os.symlink(ebuild_file,os.path.join(ebuild_dir,package.ebuild_vars['pn']+'-'+package.ebuild_vars['pv']+'.ebuild'))

#list package details, in PMS's format
def action_package(repo_location,package_name):
    defined_phases=[]
    package=find_package(repo_location,package_name[package_name.find('/')+1:])
    #output data
    for key,value in package.ebuild_vars.iteritems():
        if key=='pn' or key=='pv': #readonly vars, we cannot set these in ebuilds
            pass
        elif isinstance(value,str): #if string
            print key.upper()+'='+value.replace('\n','')
        elif isinstance(value,list) and key=='license':
            if len(value)>1:
                print "LICENSE=|| ( "+' '.join(value)+' )'
            else:
                print "LICENSE="+' '.join(value)
        elif isinstance(value,list): #list, concat items
            print key.upper()+'='+' '.join(value).replace('\n','')
    for pms_func in pms_phases:
        if hasattr(phases,pms_func):
            defined_phases.append(pms_func)
    print 'GCOMMON_PHASES='+' '.join(defined_phases)

def usage():
    print __doc__

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)<3:
            print "The 'sync' action takes the following parameters:"
            print " * remote_repository_uri"
            sys.exit(1)
        remote_repo=arguments[2]
        action_sync(repo_location,remote_repo)
    elif action=='list-categories':
        list_categories(repo_location)
    elif action=='list-packages':
        list_packages(repo_location)
    elif action=='generate-tree':
        generate_tree(repo_location)
    elif action=='package':
        if len(arguments)<3:
            print "The 'package' action takes the following parameters:"
            print " * category/package_name"
            print " * [version]"
            sys.exit(1)
        package_name=arguments[2]
        action_package(repo_location,package_name)
    elif action=='usage':
        usage()
    elif action in pms_phases and hasattr(phases,action):
        getattr(phases,action)(os.environ,repo_location)
    elif action in actions_wanted:
        raise NotImplementedError
    else:
        usage()
    sys.exit(0)

if __name__ == "__main__":
    main()