summaryrefslogtreecommitdiffstats
path: root/build_tools/bin/chk_arch_pkg.py
blob: b939f4f4d455070940935497a1f5b5caf69630f9 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/python2
#chk_arch_pkg.py uses the current dir name and finds the archlinux package url
#prints the current version of the package in archlinux
#prints the current version of the PKGBUILD in the working dir
#asks to download and expand the archlinx sources
#asks to copy the archlinux sources to the working dir
#asks to delete the downloaded and expanded sources

import os,sys
import shutil
import urllib2
import re
import subprocess

def getArchVer():
    repos=['testing', 'core', 'extra', 'community-testing', 'community', 'aur']
    website=""
    archVer=""
    aPKGBUILD=""
    global repo
    global sourceURL
    for repo in repos:
        try:
            if repo == "aur":
                url='https://aur.archlinux.org/packages/%s/' %currDirName
            else:
                url='https://www.archlinux.org/packages/%s/x86_64/%s/' %(repo, currDirName)
            website = urllib2.urlopen(url)
            #print website
            print '%s FOUND in %s:' %(currDirName, repo)
            print url
            break
        except urllib2.HTTPError, e:
            print '%s not in %s' %(currDirName, repo)

    if website:
        website_text = website.read()
        #print website_text
        try:
            if repo == "aur":
                archVerList = re.findall('<h2>Package Details: .*? (.*?)</h2>', website_text)
                archVer = archVerList[0]
            else:
                if "- Split Package Details</title>" in website_text:
                    print "This is a split package"
                    aPKGBUILD = urllib2.urlopen("https://projects.archlinux.org/svntogit/packages.git/tree/trunk/PKGBUILD?h=packages/" + currDirName)
                    aPKGBUILD_text = aPKGBUILD.read()
                    archPKGVER = re.findall('pkgver<span class="hl opt">=</span><span class="hl num">(.*?)</span>', aPKGBUILD_text)
                    archPKGREL = re.findall('pkgrel<span class="hl opt">=</span><span class="hl num">(.*?)</span>', aPKGBUILD_text)
                    archVer = archPKGVER[0] + "-" + archPKGREL[0]
                else:
                    archVerList = re.findall('<meta itemprop="softwareVersion" content="(.*?)"/>', website_text)
                    archVer = archVerList[0]
            print currDirName + " version in archlinux is:     " + archVer
        except:
            print "Unable to find archlinux version"

        try:
            if repo == "aur":
                source = re.findall('<li><a href="(.*?)">Download tarball</a></li>', website_text)
                sourceURL = "https://aur.archlinux.org" + source[0]
            else:
                if "- Split Package Details</title>" in website_text:
                    #print "Split Pkg"
                    sourceURL = "https://projects.archlinux.org/svntogit/packages.git/tree/trunk?h=packages/" + currDirName
                else:
                    source = re.findall('<a href="(.*?)" title="View source files for .*?">Source Files</a>', website_text)
                    sourceURL = source[0]
            #print sourceURL
        except:
            print "Unable to find archlinux source link"
            sourceURL = ""
    else:
        print "%s is not found in archlinux repos" %currDirName

    return archVer

def getLHVer():
    pkgfile = os.getcwd() + "/PKGBUILD"
    if not os.path.isfile(pkgfile):
        print currDirName + " version in LH PKGBUILD is:   Unknown"
        print "    Can't find",pkgfile
        #sys.exit(2)
    else:
        LHVer=""
        variables=["pkgbase", "pkgname", "pkgver", "pkgrel", "epoch"]
        # Loop over contents to get our variables
        # Use bash to do it because PKGBUILDs are very loose with their format
        for item in variables:
            v = subprocess.Popen(['/bin/bash','-c', 'source ' +
                                 pkgfile +
                                 '; echo ${' + item + '[@]}'],
                                 stdout = subprocess.PIPE,)
            value = v.communicate()[0].strip('\n')
            if item == "pkgbase":
                pkgbase = value
            elif item == "pkgname":
                pkgname = value
                pkglist = list(value.split())
            elif item == "epoch":
                if value:
                    epoch = "%s:" %value
                    LHVer=epoch
            elif item == "pkgver":
                pkgver = value
            elif item == "pkgrel":
                pkgrel = value
        LHVer=LHVer + pkgver + "-" + pkgrel
        print currDirName + " version in LH PKGBUILD is:   " + LHVer

def getSource():
    if yes_all:
        str1 = "Y"
    else:
        str1 = raw_input("\n   Press any key to download source from arch, or N to cancel: ")
    if str1 != 'N' and str1 != 'n':
        if repo == "aur":
            dlSource = sourceURL
        else:
            if "community" in repo:
                repoTag = "community"
            else:
                repoTag = "packages"
            dlSource = 'https://projects.archlinux.org/svntogit/%s.git/snapshot/%s-packages/%s.tar.gz' %(repoTag, repoTag, currDirName)

        print dlSource
        subprocess.call(["curl", "-O", dlSource])
        filename=currDirName + ".tar.gz"
        subprocess.call(["tar", "xvf", filename])

        if yes_all:
            str2 = "Y"
        else:
            str2 = raw_input("\n   Press any key to copy files to pkgdir, or N to cancel: ")
        if str2 != 'N' and str2 != 'n':
            if repo == "aur":
                srcDir = os.getcwd() + "/" + currDirName
            else:
                srcDir = os.getcwd() + "/" + repoTag + "-packages/" + currDirName + "/trunk/"
            src_files = os.listdir(srcDir)
            for file_name in src_files:
                if file_name != ".AURINFO" and file_name != ".SRCINFO":
                    full_file_name = os.path.join(srcDir, file_name)
                    if (os.path.isfile(full_file_name)):
                        shutil.copy(full_file_name, os.getcwd())
            subprocess.call("diff -q -x '__arch_LH.diff' -x '__changelog' -x '.SRCINFO' -x '.AURINFO' -x 'pkg' -x 'src' -x '*.pkg.tar.xz' -x 'packages-packages' -x 'community-packages' -x %s -x '*-package.log' -x '*-build.log' -x '%s' ./ %s > ./__arch_LH.diff" % (currDirName,filename, srcDir), shell=True)
            if os.stat(os.getcwd() + "/__arch_LH.diff").st_size == 0:
                subprocess.call("rm __arch_LH.diff", shell=True)
        else:
            sys.exit(0)
            
        if yes_all:
            str3 = "Y"
        else:
            str3 = raw_input("\n   Press any key to remove downloads, or N to cancel: ")
        if str3 != 'N' and str3 != 'n':
            subprocess.call(["rm", filename])
            if repo == "aur":
                subprocess.call(["rm", "-r", currDirName])            
            else:
                subprocess.call(["rm", "-r", repoTag + "-packages"])            
        else:
            sys.exit(0)

def main():
    global currDirName
    global yes_all

    current_folder_path, currDirName = os.path.split(os.getcwd())
    yes_all = False

    if "--yes_all" in sys.argv or "-y" in sys.argv:
        yes_all = True

    # use command line for currDirName
    if len(sys.argv) > 1:
        if not sys.argv[len(sys.argv) - 1].startswith('-'):
            currDirName = sys.argv[len(sys.argv) - 1]

    print "Searching for " + currDirName + " on archlinux.org..."
    archVer = getArchVer()
    getLHVer()
    if archVer != "" and sourceURL != "":
        getSource()

if __name__ == "__main__":
    print "--------------------------"
    main()
    print "--------------------------"