rl_ci_tools.py
author robin <robin@reportlab.com>
Mon, 19 Jun 2017 15:11:35 +0100
changeset 7 12f08e267b71
parent 6 2fe9dd0a6b76
child 8 a39bdd5144d6
permissions -rw-r--r--
add email command
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
5
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
    70
    def info(self,u,p,kind,pat,subdir=''):
0
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    71
        #self.login(u,p)
5
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
    72
        if subdir:
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
    73
            subdir += '/'
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
    74
        url = '%s/pypi/%s-info/%s%s/?json=1' % (self.root,kind,subdir,pat)
0
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    75
        resp = self.session.get(url,
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    76
                #data=dict(csrfmiddlewaretoken=self.session.cookies['csrftoken']),
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    77
                headers = dict(Referer=self.loginurl),
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    78
                )
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    79
        status_code = resp.status_code
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    80
        b = resp.content
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    81
        if debug>2:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    82
            print('!!!!!\n%r\n!!!!!'% b)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    83
        if status_code!=200:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    84
            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
    85
        I = json.loads(b)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    86
        if verbosity>1:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    87
            print('%s: %r --> %d rows' % (PROG, url, len(I)))
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    88
        return I
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    89
5
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
    90
    def upload(self,u,p,kind,fn,subdir=''):
0
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    91
        self.login(u,p)
5
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
    92
        if subdir:
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
    93
            subdir = '/' + subdir
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
    94
        url = '%s/pypi/upload-%s%s/' % (self.root,kind,subdir)
0
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    95
        files= dict(file=(os.path.basename(fn),open(fn,'rb'),'application/octet-stream'))
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    96
        resp = self.session.post(url,
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    97
                data=dict(csrfmiddlewaretoken=self.session.cookies['csrftoken']),
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    98
                files=files,
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
    99
                headers = dict(Referer=self.loginurl),
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   100
                )
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   101
        status_code = resp.status_code
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   102
        text = resp.text
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   103
        if text!='OK' or status_code!=200:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   104
            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
   105
        if verbosity:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   106
            print('%s: uploaded %r to %r.' % (PROG,fn,url))
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   107
        return resp.status_code
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   108
5
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
   109
    def clear_cache(self,u,p,fn):
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
   110
        self.login(u,p)
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
   111
        url = '%s/pypi/clear-cache/%s/' % (self.root,fn)
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
   112
        resp = self.session.post(url,
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
   113
                data=dict(csrfmiddlewaretoken=self.session.cookies['csrftoken']),
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
   114
                headers = dict(Referer=self.loginurl),
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
   115
                )
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
   116
        status_code = resp.status_code
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
   117
        text = resp.text
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
   118
        if not text.endswith('OK') or status_code!=200:
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
   119
            raise ValueError('%s: clear-cache %r failed with status_code=%r!\n%r' % (PROG,url,status_code,text))
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
   120
        if verbosity:
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
   121
            print('%s: cleared cache %r.' % (PROG,fn))
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
   122
        return resp.status_code
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
   123
7
12f08e267b71 add email command
robin <robin@reportlab.com>
parents: 6
diff changeset
   124
    def email(self,u,p,subject,faddr,taddr,body):
12f08e267b71 add email command
robin <robin@reportlab.com>
parents: 6
diff changeset
   125
        self.login(u,p)
12f08e267b71 add email command
robin <robin@reportlab.com>
parents: 6
diff changeset
   126
        url = '%s/pypi/email/' % self.root
12f08e267b71 add email command
robin <robin@reportlab.com>
parents: 6
diff changeset
   127
        resp = self.session.post(url,
12f08e267b71 add email command
robin <robin@reportlab.com>
parents: 6
diff changeset
   128
                data=dict(csrfmiddlewaretoken=self.session.cookies['csrftoken'],
12f08e267b71 add email command
robin <robin@reportlab.com>
parents: 6
diff changeset
   129
                            subject = subject,
12f08e267b71 add email command
robin <robin@reportlab.com>
parents: 6
diff changeset
   130
                            faddr = faddr,
12f08e267b71 add email command
robin <robin@reportlab.com>
parents: 6
diff changeset
   131
                            taddr = taddr,
12f08e267b71 add email command
robin <robin@reportlab.com>
parents: 6
diff changeset
   132
                            body = body.replace('\\n','\n')),
12f08e267b71 add email command
robin <robin@reportlab.com>
parents: 6
diff changeset
   133
                headers = dict(Referer=self.loginurl),
12f08e267b71 add email command
robin <robin@reportlab.com>
parents: 6
diff changeset
   134
                )
12f08e267b71 add email command
robin <robin@reportlab.com>
parents: 6
diff changeset
   135
        status_code = resp.status_code
12f08e267b71 add email command
robin <robin@reportlab.com>
parents: 6
diff changeset
   136
        text = resp.text
12f08e267b71 add email command
robin <robin@reportlab.com>
parents: 6
diff changeset
   137
        if not text.endswith('OK') or status_code!=200:
12f08e267b71 add email command
robin <robin@reportlab.com>
parents: 6
diff changeset
   138
            raise ValueError('%s: %r failed with status_code=%r!\n%r' % (PROG,url,status_code,text))
12f08e267b71 add email command
robin <robin@reportlab.com>
parents: 6
diff changeset
   139
        if verbosity:
12f08e267b71 add email command
robin <robin@reportlab.com>
parents: 6
diff changeset
   140
            print('%s: email to %r(%r) sent.' % (PROG,taddr,subject))
12f08e267b71 add email command
robin <robin@reportlab.com>
parents: 6
diff changeset
   141
        return resp.status_code
12f08e267b71 add email command
robin <robin@reportlab.com>
parents: 6
diff changeset
   142
3
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   143
    def package_version(self,u,p,pkg):
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   144
        I = self.info(u,p,'package','%s-*' % pkg)
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   145
        if not I:
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   146
            v = 'unknown'
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   147
        else:
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   148
            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
   149
        return (pkg,v)
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   150
0
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   151
def getoption(key,default=0,cnv=int):
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   152
    key = '--%s=' % key
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   153
    v = [x for x in sys.argv if x.startswith(key)]
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   154
    if v:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   155
        for x in v:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   156
            sys.argv.remove(x)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   157
        v = cnv(v[-1][len(key):])
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   158
    else:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   159
        v = default
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   160
    return v
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   161
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   162
def _file_info(fn):
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   163
    st = os.stat(fn)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   164
    return (fn,st.st_size,st.st_mtime)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   165
4
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   166
def _list_fs(patterns,recur=False):
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   167
    for pat in patterns:
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   168
        for fn in glob.glob(pat):
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   169
            if not recur:
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   170
                yield fn
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   171
            elif os.path.isdir(fn):
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   172
                for r,s,F in os.walk(fn):
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   173
                    yield r
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   174
                    for f in F:
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   175
                        yield os.path.join(r,f)
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   176
            else:
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   177
                yield fn
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   178
3
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   179
def tabulate(I,
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   180
                hdrs=['Name','Length',(5*' ')+'Modified'],
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   181
                fmtmpl8='{:<%d}\x20{:>%d}\x20\x20{:<%d}',
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   182
                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
   183
                ):
0
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   184
    if I:
3
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   185
        rows = [hdrs]
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   186
        for row in I:
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   187
            rows.append([c(r) for c,r in zip(cnvf,row)])
0
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   188
        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
   189
        if debug>3:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   190
            print('tabluate: rows=%s' % repr(rows))
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   191
            print('tabluate: W=%s' % repr(W))
3
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   192
        fmt = fmtmpl8 % tuple(W)
0
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   193
        print('\n'.join(fmt.format(*i) for i in rows))
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   194
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   195
def main():
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   196
    u = os.environ.get('CITOOLS_USER','beta')
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   197
    p = os.environ.get('CITOOLS_PASSWORD','???')
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   198
    global debug, verbosity
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   199
    debug = getoption('debug')
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   200
    verbosity = getoption('verbosity')
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   201
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   202
    if debug>3:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   203
        import logging
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   204
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   205
        # These two lines enable debugging at httplib level (requests->urllib3->http.client)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   206
        # 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
   207
        # The only thing missing will be the response.body which is not logged.
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   208
        try:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   209
            import http.client as http_client
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   210
        except ImportError:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   211
            # Python 2
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   212
            import httplib as http_client
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   213
        http_client.HTTPConnection.debuglevel = 1
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   214
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   215
        # You must initialize logging, otherwise you'll not see debug output.
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   216
        logging.basicConfig()
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   217
        logging.getLogger().setLevel(logging.DEBUG)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   218
        requests_log = logging.getLogger("requests.packages.urllib3")
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   219
        requests_log.setLevel(logging.DEBUG)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   220
        requests_log.propagate = True
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   221
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   222
    dst = getoption('dst',None,cnv=str)
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   223
    try:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   224
        cmd = sys.argv[1]
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   225
    except:
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   226
        cmd = 'help'
3
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   227
    if cmd=='env':
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   228
        print('Environment')
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   229
        print('===========')
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   230
        I = list(sorted(os.environ.iteritems()))
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   231
        i = max([len(i[0]) for i in I])
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   232
        print(('{:<%d}  {}' % i).format('Key','Value'))
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   233
        fmt  = '{:<%d} = {}' % i
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   234
        for i in I:
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   235
            print(fmt.format(*i))
0
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   236
    elif cmd=='info':
4
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   237
        recur = [fn for fn in sys.argv[2:] if fn=='--recur']
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   238
        if recur:
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   239
            map(sys.argv.remove,recur)
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   240
            recur = True
1899f4fe9c9b make info recursive
robin <robin@reportlab.com>
parents: 3
diff changeset
   241
        tabulate([_file_info(i) for i in _list_fs(sys.argv[2:],recur)])
0
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   242
    elif cmd=='help':
3
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   243
        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
   244
    else:
3
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   245
        pypi = PyPiRequestor(debug=debug)
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   246
        if cmd=='test':
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   247
            status_code = pypi.login(u,p)
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   248
            if debug:
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   249
                print('status=%s' % status_code)
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   250
        elif cmd.startswith('download-'):
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   251
            kind = cmd.split('-')[1]
5
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
   252
            if not kind in ('resources','packages', 'caches'):
3
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   253
                raise ValueError('%s: invalid download kind: %r' % (PROG,kind))
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   254
            if dst and not os.path.isdir(dst):
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   255
                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
   256
            for fn in sys.argv[2:]:
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   257
                pypi.download(u,p,kind,fn,dst)
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   258
        elif cmd.endswith('-info'):
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   259
            kind = cmd.split('-')[0]
5
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
   260
            if not kind in ('resource','package', 'cache'):
3
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   261
                raise ValueError('%s: invalid info kind: %r' % (PROG,kind))
6
2fe9dd0a6b76 fix subdir default
robin <robin@reportlab.com>
parents: 5
diff changeset
   262
            subdir = getoption('subdir','',str) if kind=='cache' else ''
5
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
   263
            tabulate([i for fn in sys.argv[2:] for i in pypi.info(u,p,kind,fn,subdir)])
3
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   264
        elif cmd.startswith('upload-'):
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   265
            kind = cmd.split('-')[1]
5
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
   266
            if not kind in ('resources','packages', 'caches'):
3
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   267
                raise ValueError('%s: invalid upload kind: %r' % (PROG,kind))
6
2fe9dd0a6b76 fix subdir default
robin <robin@reportlab.com>
parents: 5
diff changeset
   268
            subdir = getoption('subdir','',str) if kind=='caches' else ''
3
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   269
            for pat in sys.argv[2:]:
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   270
                for fn in glob.glob(pat):
5
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
   271
                    pypi.upload(u,p,kind[:-1],fn,subdir)
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
   272
        elif cmd=='clear-cache':
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
   273
            for fn in sys.argv[2:]:
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
   274
                pypi.clear_cache(u,p,fn)
7
12f08e267b71 add email command
robin <robin@reportlab.com>
parents: 6
diff changeset
   275
        elif cmd=='email':
12f08e267b71 add email command
robin <robin@reportlab.com>
parents: 6
diff changeset
   276
            subject = getoption('subject','unknown',str)
12f08e267b71 add email command
robin <robin@reportlab.com>
parents: 6
diff changeset
   277
            faddr = getoption('faddr','bitbucket@reportlab.com',str)
12f08e267b71 add email command
robin <robin@reportlab.com>
parents: 6
diff changeset
   278
            taddr = getoption('taddr','live_errors@reportlab.com',str)
12f08e267b71 add email command
robin <robin@reportlab.com>
parents: 6
diff changeset
   279
            body = getoption('body','live_errors@reportlab.com',str)
12f08e267b71 add email command
robin <robin@reportlab.com>
parents: 6
diff changeset
   280
            pypi.email(u,p,subject,faddr,taddr,body)
3
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   281
        elif cmd=='package-version':
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   282
            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
   283
                    hdrs = ['Package','Version'],
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   284
                    fmtmpl8 = '{:<%d}  {:>%d}',
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   285
                    cnvf = (str,str),
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   286
                    )
dd3ccb935508 added env and package-version commands
robin <robin@reportlab.com>
parents: 2
diff changeset
   287
        else:
5
539b3d5f515e added caches handling
robin <robin@reportlab.com>
parents: 4
diff changeset
   288
            raise ValueError('%s: unknown command %r' % (PROG,cmd))
0
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   289
if __name__=='__main__':
664ae0993bd1 first checkin
robin <robin@reportlab.com>
parents:
diff changeset
   290
    main()