~elementary-os/ubuntu-package-imports/apt-to-bzr-importer-old

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
#!/bin/bash

# checks if any imports should be performed and performs them
# accepts package names as command-line parameters
# if none given, downloads the lists from a bzr branch
# Defaunt branch is lp:ubuntu-package-imports/elementary-import-list-distroversion
# The default value is overideable, see below

# export something different from your shell to override these values
export bzr_branch_prefix=${bzr_branch_prefix:-'lp:~elementary-os/ubuntu-package-imports/'}
export distro_version=${distro_version:-$(lsb_release -cs)}

# check for dependencies
dependency_list='devscripts dpkg-dev build-essential bzr quilt lsb-release'
for package in $dependency_list; do
dpkg -L "$package" >/dev/null 2>&1 || missing_dependencies="$missing_dependencies $package"
done

if [ "$missing_dependencies" != "" ]; then
    echo "Missing dependencies! Please install these packages to proceed:
$missing_dependencies" > /dev/stderr
    exit 1
fi

if (echo "$1" | grep no-apt-update); then
    no_apt_update=true
    shift
fi

# create and enter temporary directory
tmp_dir=$(mktemp -d)
cd $tmp_dir

# make sure temporary directory gets removed afterward
cleanup() {
rm -rf $tmp_dir
}

trap cleanup EXIT

if [ -z "$@" ]; then
    LISTS_BRANCH=${LISTS_BRANCH:-"lp:ubuntu-package-imports/elementary-import-list-$distro_version"}
    target_folder=$(echo "$LISTS_BRANCH" | rev | cut -d '/' -f 1 | rev )
    echo "No packages specificed on command line.
Using package list from $LISTS_BRANCH
You can override this by exporting LISTS_BRANCH variable from your shell." > /dev/stderr 
    rm -rf "$target_folder"
    bzr checkout --lightweight "$LISTS_BRANCH"
    package_list="$(cat $target_folder/packages_to_import)"
else
    package_list="$@"
fi

# don't let l10n interfere with our grepping
export LC_ALL=C

# check if any source arepos are enabled
if ! grep -E --silent '^ *deb-src' /etc/apt/sources.list /etc/apt/sources.list.d/*; then
    echo "You don't have any source code repositories enabled.
    Please enable them via software-center or by uncommenting lines starting with
    'deb-src' in sources.list and sources.list.d/" > /dev/stderr 
    exit 1
fi

# update info about available packages, unless --no-apt-update is given
# try to use aptdcon which doesn't propmt for sudo password and fall back to apt
if ! [ "$no_apt_update" = true ]; then
    aptdcon --refresh > /dev/null 2>&1 || apt-get update || echo "Failed to update package version information.
    Continuing anyway, but imported packages may be outdated."
fi

find_greatest_version() {
# returns the greatest version from the given list of versions
# usage: find_greatest_version version1 [version2...]
greatest_version="$1"
while true; do
    shift || break
    if ( dpkg --compare-versions "$1" '>>' "$greatest_version" ); then
        greatest_version="$1"
    fi
done
echo "$greatest_version"
}

get_latest_package_version() {
# gets latest version of a source package available on the system
package="$1"
version_list=$( apt-cache showsrc "$package" \
              | grep '^Version:' \
              | sed --regexp-extended 's/Version: //' )
find_greatest_version $version_list
}

package_version_already_imported() {
package="$1"
version="$2"
rm -rf "$package-$distro_version"
bzr checkout --lightweight "$bzr_branch_prefix""$package-$distro_version" &&
grep --silent " ($version) " "$package-$distro_version"/debian/changelog
}

failed_to_upload() {
echo "Failed to commit the changes or upload the code to the bazaar branch.
Either your internet connection suddenly went out, or it's caused by a bug.
If your internet connection works, please report this failure at:
https://bugs.launchpad.net/ubuntu-package-imports/+filebug" > /dev/stderr
exit 1
}

# main cycle
for package in $package_list; do
    latest_version="$(get_latest_package_version $package)"
    # safeguard against importing a patched version
    if ( echo "$latest_version" | grep -q ${patched_version_suffix:-elementary} ); then
        echo "ERROR: looks like the version you're trying to import is already patched.
Aborted."
    exit 1
    fi
    if ! package_version_already_imported "$package" "$latest_version" ; then
        rm -rf "$package"*
        apt-get source "$package" || {
            echo "Failed to download source code package $package" > /dev/stderr 
            exit 1
}

        rm -Rf "$package"-*
        dpkg-source -x --skip-patches --no-preparation "$package"_*.dsc

        cd "$package"-* || { 
            echo "Failed to cd to the downloaded source code directory.
Most likely this is a bug in the script.
Please report this failure at:
https://bugs.launchpad.net/ubuntu-package-imports/+filebug" > /dev/stderr
            exit 1
            }

        quilt pop -a || true
        rm -rf .pc

        if bzr checkout --lightweight "$bzr_branch_prefix""$package-$distro_version" ../"$package-$distro_version"; then
            # checkout succeeded
            cp -r ../"$package-$distro_version"/.bzr .bzr
            bzr add
            debcommit -m "updated to version $latest_version" || failed_to_upload
        else
            # checkout failed; assuming the branch doesn't exist, which means
            # this is the first import of this package
            bzr init
            bzr add
            bzr commit -m "Initial import, version $latest_version"
            bzr push "$bzr_branch_prefix""$package-$distro_version" || failed_to_upload
        fi
    fi
done

exit 0