rl_ci_tools.py
author robin <robin@reportlab.com>
Thu, 15 Jun 2017 11:56:32 +0100
changeset 4 1899f4fe9c9b
parent 3 dd3ccb935508
child 5 539b3d5f515e
permissions -rw-r--r--
make info recursive
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
     1
VERSION='0.0.2'
0
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
     2
import os, sys, glob, time, json
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
     3
PROG=os.path.basename(sys.argv[0])
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
     4
debug=verbosity=0
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
     5
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
     6
class PyPiRequestor():
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
     7
    scheme = 'https'
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
     8
    host = os.environ.get('CITOOLS_SERVER','www.reportlab.com')
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
     9
    root = '%s://%s' % (scheme,host)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    10
    loginurl = "%s/accounts/login/" % root
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    11
    def __init__(self,debug=0):
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    12
        self.debug = debug
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    13
        import requests
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    14
        self.session = requests.session()
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    15
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    16
    def login(self,u,p,nxt='/test-7491/'):
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    17
        s = self.session
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    18
        resp = s.get(self.loginurl)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    19
        loginpage = resp.text
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    20
        if self.debug>1:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    21
            if self.debug>2: print('=============loginpage\n%r' % loginpage)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    22
        resp = s.post(self.loginurl,
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    23
                        data=dict(
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    24
                                csrfmiddlewaretoken=s.cookies['csrftoken'],
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    25
                                username=u,
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    26
                                password=p,
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    27
                                next=nxt,
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    28
                                ),
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    29
                        headers=dict(
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    30
                            Referer=self.loginurl,
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    31
                            ),
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    32
                        )
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    33
        text = resp.text
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    34
        status_code = resp.status_code
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    35
        if debug>2:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    36
            print('!!!!!\n%s\n!!!!!'% text)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    37
            print('%s: test-7491 csrftoken=%r' % (PROG,resp.cookies.get('csrftoken','???')))
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    38
        if text!='I am alive!' or status_code!=200:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    39
            raise ValueError('%s: login at %r failed with status_code=%r' % (PROG,self.loginurl,status_code))
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    40
        elif verbosity>=2:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    41
            print('%s: logged in OK' % PROG)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    42
        return status_code
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    43
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    44
    def _download(self,u,p, kind, fn, dst):
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    45
        self.login(u,p)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    46
        base = '%s/pypi/%s/' % (self.root,kind)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    47
        url = base + fn + '/'
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    48
        resp = self.session.get(url,
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    49
                data=dict(csrfmiddlewaretoken=self.session.cookies['csrftoken']),
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    50
                headers = dict(Referer=self.loginurl),
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    51
                )
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    52
        status_code = resp.status_code
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    53
        b = resp.content
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    54
        if debug>2:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    55
            print('!!!!!\n%r\n!!!!!'% b)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    56
        if status_code!=200:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    57
            raise ValueError('%s: download %r failed with status_code=%r!\n%r' % (PROG,url,status_code,b))
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    58
        if dst:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    59
            fn = os.path.join(dst,fn)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    60
        with open(fn,'wb') as f:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    61
            f.write(b)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    62
        if verbosity:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    63
            print('%s: %r(%d bytes) downloaded from %r.' % (PROG, fn, len(b),base))
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    64
        return resp.status_code
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    65
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    66
    def download(self,u,p,kind, fn, dst):
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    67
        for i in self.info(u,p,kind[:-1],fn):
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    68
            self._download(u,p,kind,i[0],dst)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    69
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    70
    def info(self,u,p,kind,pat):
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    71
        #self.login(u,p)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    72
        url = '%s/pypi/%s-info/%s/?json=1' % (self.root,kind,pat)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    73
        resp = self.session.get(url,
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    74
                #data=dict(csrfmiddlewaretoken=self.session.cookies['csrftoken']),
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    75
                headers = dict(Referer=self.loginurl),
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    76
                )
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    77
        status_code = resp.status_code
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    78
        b = resp.content
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    79
        if debug>2:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    80
            print('!!!!!\n%r\n!!!!!'% b)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    81
        if status_code!=200:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    82
            raise ValueError('%s: request %r failed with status_code=%r!\n%r' % (PROG,url,status_code,b))
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    83
        I = json.loads(b)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    84
        if verbosity>1:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    85
            print('%s: %r --> %d rows' % (PROG, url, len(I)))
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    86
        return I
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    87
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    88
    def upload(self,u,p,kind,fn):
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    89
        self.login(u,p)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    90
        url = '%s/pypi/upload-%s/' % (self.root,kind)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    91
        files= dict(file=(os.path.basename(fn),open(fn,'rb'),'application/octet-stream'))
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    92
        resp = self.session.post(url,
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    93
                data=dict(csrfmiddlewaretoken=self.session.cookies['csrftoken']),
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    94
                files=files,
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    95
                headers = dict(Referer=self.loginurl),
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    96
                )
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    97
        status_code = resp.status_code
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    98
        text = resp.text
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    99
        if text!='OK' or status_code!=200:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   100
            raise ValueError('%s: upload %r failed with status_code=%r!\n%r' % (PROG,url,status_code,text))
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   101
        if verbosity:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   102
            print('%s: uploaded %r to %r.' % (PROG,fn,url))
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   103
        return resp.status_code
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   104
3
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   105
    def package_version(self,u,p,pkg):
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   106
        I = self.info(u,p,'package','%s-*' % pkg)
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   107
        if not I:
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   108
            v = 'unknown'
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   109
        else:
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   110
            v = '.'.join(map(str,list(sorted([tuple([int(x) for x in i[0].split('-',2)[1].split('.') if x and x[0] in '0123456789']) for i in I]))[-1]))
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   111
        return (pkg,v)
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   112
0
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   113
def getoption(key,default=0,cnv=int):
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   114
    key = '--%s=' % key
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   115
    v = [x for x in sys.argv if x.startswith(key)]
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   116
    if v:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   117
        for x in v:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   118
            sys.argv.remove(x)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   119
        v = cnv(v[-1][len(key):])
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   120
    else:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   121
        v = default
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   122
    return v
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   123
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   124
def _file_info(fn):
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   125
    st = os.stat(fn)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   126
    return (fn,st.st_size,st.st_mtime)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   127
4
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   128
def _list_fs(patterns,recur=False):
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   129
    for pat in patterns:
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   130
        for fn in glob.glob(pat):
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   131
            if not recur:
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   132
                yield fn
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   133
            elif os.path.isdir(fn):
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   134
                for r,s,F in os.walk(fn):
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   135
                    yield r
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   136
                    for f in F:
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   137
                        yield os.path.join(r,f)
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   138
            else:
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   139
                yield fn
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   140
3
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   141
def tabulate(I,
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   142
                hdrs=['Name','Length',(5*' ')+'Modified'],
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   143
                fmtmpl8='{:<%d}\x20{:>%d}\x20\x20{:<%d}',
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   144
                cnvf=(str,str,lambda t: time.strftime('%Y%m%d %H:%M:%S',time.localtime(t))),
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   145
                ):
0
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   146
    if I:
3
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   147
        rows = [hdrs]
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   148
        for row in I:
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   149
            rows.append([c(r) for c,r in zip(cnvf,row)])
0
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   150
        W = [max(map(len,col)) for col in [list(i) for i in zip(*rows)]]
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   151
        if debug>3:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   152
            print('tabluate: rows=%s' % repr(rows))
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   153
            print('tabluate: W=%s' % repr(W))
3
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   154
        fmt = fmtmpl8 % tuple(W)
0
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   155
        print('\n'.join(fmt.format(*i) for i in rows))
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   156
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   157
def main():
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   158
    u = os.environ.get('CITOOLS_USER','beta')
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   159
    p = os.environ.get('CITOOLS_PASSWORD','???')
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   160
    global debug, verbosity
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   161
    debug = getoption('debug')
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   162
    verbosity = getoption('verbosity')
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   163
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   164
    if debug>3:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   165
        import logging
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   166
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   167
        # These two lines enable debugging at httplib level (requests->urllib3->http.client)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   168
        # You will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA.
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   169
        # The only thing missing will be the response.body which is not logged.
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   170
        try:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   171
            import http.client as http_client
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   172
        except ImportError:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   173
            # Python 2
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   174
            import httplib as http_client
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   175
        http_client.HTTPConnection.debuglevel = 1
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   176
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   177
        # You must initialize logging, otherwise you'll not see debug output.
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   178
        logging.basicConfig()
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   179
        logging.getLogger().setLevel(logging.DEBUG)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   180
        requests_log = logging.getLogger("requests.packages.urllib3")
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   181
        requests_log.setLevel(logging.DEBUG)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   182
        requests_log.propagate = True
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   183
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   184
    dst = getoption('dst',None,cnv=str)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   185
    try:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   186
        cmd = sys.argv[1]
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   187
    except:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   188
        cmd = 'help'
3
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   189
    if cmd=='env':
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   190
        print('Environment')
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   191
        print('===========')
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   192
        I = list(sorted(os.environ.iteritems()))
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   193
        i = max([len(i[0]) for i in I])
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   194
        print(('{:<%d}  {}' % i).format('Key','Value'))
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   195
        fmt  = '{:<%d} = {}' % i
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   196
        for i in I:
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   197
            print(fmt.format(*i))
0
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   198
    elif cmd=='info':
4
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   199
        recur = [fn for fn in sys.argv[2:] if fn=='--recur']
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   200
        if recur:
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   201
            map(sys.argv.remove,recur)
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   202
            recur = True
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   203
        tabulate([_file_info(i) for i in _list_fs(sys.argv[2:],recur)])
0
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   204
    elif cmd=='help':
3
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   205
        print('Usage %s [test|info|env|download-[resources|packages]|upload-[resources|packages]|[packages|resources]-info] path....' % PROG)
0
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   206
    else:
3
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   207
        pypi = PyPiRequestor(debug=debug)
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   208
        if cmd=='test':
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   209
            status_code = pypi.login(u,p)
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   210
            if debug:
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   211
                print('status=%s' % status_code)
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   212
        elif cmd.startswith('download-'):
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   213
            kind = cmd.split('-')[1]
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   214
            if not kind in ('resources','packages'):
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   215
                raise ValueError('%s: invalid download kind: %r' % (PROG,kind))
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   216
            if dst and not os.path.isdir(dst):
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   217
                raise ValueError('%s: %r is not a directory!' % (PROG,dst))
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   218
            for fn in sys.argv[2:]:
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   219
                pypi.download(u,p,kind,fn,dst)
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   220
        elif cmd.endswith('-info'):
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   221
            kind = cmd.split('-')[0]
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   222
            if not kind in ('resource','package'):
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   223
                raise ValueError('%s: invalid info kind: %r' % (PROG,kind))
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   224
            tabulate([i for fn in sys.argv[2:] for i in pypi.info(u,p,kind,fn)])
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   225
        elif cmd.startswith('upload-'):
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   226
            kind = cmd.split('-')[1]
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   227
            if not kind in ('resources','packages'):
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   228
                raise ValueError('%s: invalid upload kind: %r' % (PROG,kind))
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   229
            for pat in sys.argv[2:]:
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   230
                for fn in glob.glob(pat):
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   231
                    pypi.upload(u,p,kind[:-1],fn)
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   232
        elif cmd=='package-version':
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   233
            tabulate([pypi.package_version(u,p,fn) for fn in sys.argv[2:]],
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   234
                    hdrs = ['Package','Version'],
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   235
                    fmtmpl8 = '{:<%d}  {:>%d}',
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   236
                    cnvf = (str,str),
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   237
                    )
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   238
        else:
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   239
            raise ValueError('%s: nknown command %r' % (PROG,cmd))
0
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   240
if __name__=='__main__':
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   241
    main()