-
Notifications
You must be signed in to change notification settings - Fork 113
/
pkgsetup.go
170 lines (132 loc) · 4.44 KB
/
pkgsetup.go
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
// Copyright 2019 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/go-python/gopy/bind"
)
// 1 = pkg name, 2 = -user, 3 = version 4 = author, 5 = email, 6 = desc, 7 = url
const (
setupTempl = `import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
class BinaryDistribution(setuptools.Distribution):
def has_ext_modules(_):
return True
setuptools.setup(
name="%[1]s%[2]s",
version="%[3]s",
author="%[4]s",
author_email="%[5]s",
description="%[6]s",
long_description=long_description,
long_description_content_type="text/markdown",
url="%[7]s",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: BSD License",
"Operating System :: OS Independent",
],
include_package_data=True,
distclass=BinaryDistribution,
)
`
manifestTempl = `global-include *.so *.py *.pyd
global-exclude build.py
`
// 1 = pkg name
bsdLicense = `BSD 3-Clause License
Copyright (c) 2018, The %[1]s Authors
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
`
// 1 = pkg name, 2 = desc
readmeTempl = `# %[1]s
%[2]s
`
// 1 = pkg name, 2 = cmd, 3 = gencmd, 4 = vm (exe only)
makefileTempl = `# Makefile for gopy pkg generation of python bindings to %[1]s
# File is generated by gopy (will not be overwritten though)
# %[2]s
PYTHON=%[4]s
PIP=$(PYTHON) -m pip
all: gen
install: install-pkg install-exe
gen:
%[3]s
build:
$(MAKE) -C %[1]s build
install-pkg:
# this does a local install of the package, building the sdist and then directly installing it
rm -rf dist build */*.egg-info *.egg-info
$(PYTHON) setup.py sdist
$(PIP) install dist/*.tar.gz
install-exe:
# install executable into /usr/local/bin
cp %[1]s/py%[1]s /usr/local/bin/
`
)
// GenPyPkgSetup generates python package setup files
func GenPyPkgSetup(cfg *BuildCfg, user, version, author, email, desc, url string) error {
os.Chdir(cfg.OutputDir)
dashUser := user
if user != "" {
dashUser = "-" + user
}
sf, err := os.Create(filepath.Join(cfg.OutputDir, "setup.py"))
if err != nil {
return err
}
fmt.Fprintf(sf, setupTempl, cfg.Name, dashUser, version, author, email, desc, url)
sf.Close()
mi, err := os.Create(filepath.Join(cfg.OutputDir, "MANIFEST.in"))
if err != nil {
return err
}
fmt.Fprintf(mi, manifestTempl)
mi.Close()
lf, err := os.Create(filepath.Join(cfg.OutputDir, "LICENSE"))
if err != nil {
return err
}
fmt.Fprintf(lf, bsdLicense, cfg.Name)
lf.Close()
rf, err := os.Create(filepath.Join(cfg.OutputDir, "README.md"))
if err != nil {
return err
}
fmt.Fprintf(rf, readmeTempl, cfg.Name, desc)
rf.Close()
_, pyonly := filepath.Split(cfg.VM)
gencmd := bind.CmdStrToMakefile(cfg.Cmd)
mf, err := os.Create(filepath.Join(cfg.OutputDir, "Makefile"))
if err != nil {
return err
}
fmt.Fprintf(mf, makefileTempl, cfg.Name, cfg.Cmd, gencmd, pyonly)
mf.Close()
return err
}