##// END OF EJS Templates
rust: extract function to convert Path to platform CString...
Yuya Nishihara -
r35649:edbe11cf default
parent child Browse files
Show More
@@ -1,225 +1,225 b''
1 // main.rs -- Main routines for `hg` program
1 // main.rs -- Main routines for `hg` program
2 //
2 //
3 // Copyright 2017 Gregory Szorc <gregory.szorc@gmail.com>
3 // Copyright 2017 Gregory Szorc <gregory.szorc@gmail.com>
4 //
4 //
5 // This software may be used and distributed according to the terms of the
5 // This software may be used and distributed according to the terms of the
6 // GNU General Public License version 2 or any later version.
6 // GNU General Public License version 2 or any later version.
7
7
8 extern crate libc;
8 extern crate libc;
9 extern crate cpython;
9 extern crate cpython;
10 extern crate python27_sys;
10 extern crate python27_sys;
11
11
12 use cpython::{NoArgs, ObjectProtocol, PyModule, PyResult, Python};
12 use cpython::{NoArgs, ObjectProtocol, PyModule, PyResult, Python};
13 use libc::{c_char, c_int};
13 use libc::{c_char, c_int};
14
14
15 use std::env;
15 use std::env;
16 use std::path::PathBuf;
16 use std::path::PathBuf;
17 use std::ffi::CString;
17 use std::ffi::{CString, OsStr};
18 #[cfg(target_family = "unix")]
18 #[cfg(target_family = "unix")]
19 use std::os::unix::ffi::OsStringExt;
19 use std::os::unix::ffi::OsStringExt;
20
20
21 #[derive(Debug)]
21 #[derive(Debug)]
22 struct Environment {
22 struct Environment {
23 _exe: PathBuf,
23 _exe: PathBuf,
24 python_exe: PathBuf,
24 python_exe: PathBuf,
25 python_home: PathBuf,
25 python_home: PathBuf,
26 mercurial_modules: PathBuf,
26 mercurial_modules: PathBuf,
27 }
27 }
28
28
29 /// Run Mercurial locally from a source distribution or checkout.
29 /// Run Mercurial locally from a source distribution or checkout.
30 ///
30 ///
31 /// hg is <srcdir>/rust/target/<target>/hg
31 /// hg is <srcdir>/rust/target/<target>/hg
32 /// Python interpreter is detected by build script.
32 /// Python interpreter is detected by build script.
33 /// Python home is relative to Python interpreter.
33 /// Python home is relative to Python interpreter.
34 /// Mercurial files are relative to hg binary, which is relative to source root.
34 /// Mercurial files are relative to hg binary, which is relative to source root.
35 #[cfg(feature = "localdev")]
35 #[cfg(feature = "localdev")]
36 fn get_environment() -> Environment {
36 fn get_environment() -> Environment {
37 let exe = env::current_exe().unwrap();
37 let exe = env::current_exe().unwrap();
38
38
39 let mut mercurial_modules = exe.clone();
39 let mut mercurial_modules = exe.clone();
40 mercurial_modules.pop(); // /rust/target/<target>
40 mercurial_modules.pop(); // /rust/target/<target>
41 mercurial_modules.pop(); // /rust/target
41 mercurial_modules.pop(); // /rust/target
42 mercurial_modules.pop(); // /rust
42 mercurial_modules.pop(); // /rust
43 mercurial_modules.pop(); // /
43 mercurial_modules.pop(); // /
44
44
45 let python_exe: &'static str = env!("PYTHON_INTERPRETER");
45 let python_exe: &'static str = env!("PYTHON_INTERPRETER");
46 let python_exe = PathBuf::from(python_exe);
46 let python_exe = PathBuf::from(python_exe);
47
47
48 let mut python_home = python_exe.clone();
48 let mut python_home = python_exe.clone();
49 python_home.pop();
49 python_home.pop();
50
50
51 // On Windows, python2.7.exe exists at the root directory of the Python
51 // On Windows, python2.7.exe exists at the root directory of the Python
52 // install. Everywhere else, the Python install root is one level up.
52 // install. Everywhere else, the Python install root is one level up.
53 if !python_exe.ends_with("python2.7.exe") {
53 if !python_exe.ends_with("python2.7.exe") {
54 python_home.pop();
54 python_home.pop();
55 }
55 }
56
56
57 Environment {
57 Environment {
58 _exe: exe.clone(),
58 _exe: exe.clone(),
59 python_exe: python_exe,
59 python_exe: python_exe,
60 python_home: python_home,
60 python_home: python_home,
61 mercurial_modules: mercurial_modules.to_path_buf(),
61 mercurial_modules: mercurial_modules.to_path_buf(),
62 }
62 }
63 }
63 }
64
64
65 fn cstring_from_os<T: AsRef<OsStr>>(s: T) -> CString {
66 CString::new(s.as_ref().to_str().unwrap()).unwrap()
67 }
68
65 // On UNIX, argv starts as an array of char*. So it is easy to convert
69 // On UNIX, argv starts as an array of char*. So it is easy to convert
66 // to C strings.
70 // to C strings.
67 #[cfg(target_family = "unix")]
71 #[cfg(target_family = "unix")]
68 fn args_to_cstrings() -> Vec<CString> {
72 fn args_to_cstrings() -> Vec<CString> {
69 env::args_os()
73 env::args_os()
70 .map(|a| CString::new(a.into_vec()).unwrap())
74 .map(|a| CString::new(a.into_vec()).unwrap())
71 .collect()
75 .collect()
72 }
76 }
73
77
74 // TODO Windows support is incomplete. We should either use env::args_os()
78 // TODO Windows support is incomplete. We should either use env::args_os()
75 // (or call into GetCommandLineW() + CommandLinetoArgvW()), convert these to
79 // (or call into GetCommandLineW() + CommandLinetoArgvW()), convert these to
76 // PyUnicode instances, and pass these into Python/Mercurial outside the
80 // PyUnicode instances, and pass these into Python/Mercurial outside the
77 // standard PySys_SetArgvEx() mechanism. This will allow us to preserve the
81 // standard PySys_SetArgvEx() mechanism. This will allow us to preserve the
78 // raw bytes (since PySys_SetArgvEx() is based on char* and can drop wchar
82 // raw bytes (since PySys_SetArgvEx() is based on char* and can drop wchar
79 // data.
83 // data.
80 //
84 //
81 // For now, we use env::args(). This will choke on invalid UTF-8 arguments.
85 // For now, we use env::args(). This will choke on invalid UTF-8 arguments.
82 // But it is better than nothing.
86 // But it is better than nothing.
83 #[cfg(target_family = "windows")]
87 #[cfg(target_family = "windows")]
84 fn args_to_cstrings() -> Vec<CString> {
88 fn args_to_cstrings() -> Vec<CString> {
85 env::args().map(|a| CString::new(a).unwrap()).collect()
89 env::args().map(|a| CString::new(a).unwrap()).collect()
86 }
90 }
87
91
88 fn set_python_home(env: &Environment) {
92 fn set_python_home(env: &Environment) {
89 let raw = CString::new(env.python_home.to_str().unwrap())
93 let raw = cstring_from_os(&env.python_home).into_raw();
90 .unwrap()
91 .into_raw();
92 unsafe {
94 unsafe {
93 python27_sys::Py_SetPythonHome(raw);
95 python27_sys::Py_SetPythonHome(raw);
94 }
96 }
95 }
97 }
96
98
97 fn update_encoding(_py: Python, _sys_mod: &PyModule) {
99 fn update_encoding(_py: Python, _sys_mod: &PyModule) {
98 // Call sys.setdefaultencoding("undefined") if HGUNICODEPEDANTRY is set.
100 // Call sys.setdefaultencoding("undefined") if HGUNICODEPEDANTRY is set.
99 let pedantry = env::var("HGUNICODEPEDANTRY").is_ok();
101 let pedantry = env::var("HGUNICODEPEDANTRY").is_ok();
100
102
101 if pedantry {
103 if pedantry {
102 // site.py removes the sys.setdefaultencoding attribute. So we need
104 // site.py removes the sys.setdefaultencoding attribute. So we need
103 // to reload the module to get a handle on it. This is a lesser
105 // to reload the module to get a handle on it. This is a lesser
104 // used feature and we'll support this later.
106 // used feature and we'll support this later.
105 // TODO support this
107 // TODO support this
106 panic!("HGUNICODEPEDANTRY is not yet supported");
108 panic!("HGUNICODEPEDANTRY is not yet supported");
107 }
109 }
108 }
110 }
109
111
110 fn update_modules_path(env: &Environment, py: Python, sys_mod: &PyModule) {
112 fn update_modules_path(env: &Environment, py: Python, sys_mod: &PyModule) {
111 let sys_path = sys_mod.get(py, "path").unwrap();
113 let sys_path = sys_mod.get(py, "path").unwrap();
112 sys_path
114 sys_path
113 .call_method(py, "insert", (0, env.mercurial_modules.to_str()), None)
115 .call_method(py, "insert", (0, env.mercurial_modules.to_str()), None)
114 .expect("failed to update sys.path to location of Mercurial modules");
116 .expect("failed to update sys.path to location of Mercurial modules");
115 }
117 }
116
118
117 fn run() -> Result<(), i32> {
119 fn run() -> Result<(), i32> {
118 let env = get_environment();
120 let env = get_environment();
119
121
120 //println!("{:?}", env);
122 //println!("{:?}", env);
121
123
122 // Tell Python where it is installed.
124 // Tell Python where it is installed.
123 set_python_home(&env);
125 set_python_home(&env);
124
126
125 // Set program name. The backing memory needs to live for the duration of the
127 // Set program name. The backing memory needs to live for the duration of the
126 // interpreter.
128 // interpreter.
127 //
129 //
128 // TODO consider storing this in a static or associating with lifetime of
130 // TODO consider storing this in a static or associating with lifetime of
129 // the Python interpreter.
131 // the Python interpreter.
130 //
132 //
131 // Yes, we use the path to the Python interpreter not argv[0] here. The
133 // Yes, we use the path to the Python interpreter not argv[0] here. The
132 // reason is because Python uses the given path to find the location of
134 // reason is because Python uses the given path to find the location of
133 // Python files. Apparently we could define our own ``Py_GetPath()``
135 // Python files. Apparently we could define our own ``Py_GetPath()``
134 // implementation. But this may require statically linking Python, which is
136 // implementation. But this may require statically linking Python, which is
135 // not desirable.
137 // not desirable.
136 let program_name = CString::new(env.python_exe.to_str().unwrap())
138 let program_name = cstring_from_os(&env.python_exe).as_ptr();
137 .unwrap()
138 .as_ptr();
139 unsafe {
139 unsafe {
140 python27_sys::Py_SetProgramName(program_name as *mut i8);
140 python27_sys::Py_SetProgramName(program_name as *mut i8);
141 }
141 }
142
142
143 unsafe {
143 unsafe {
144 python27_sys::Py_Initialize();
144 python27_sys::Py_Initialize();
145 }
145 }
146
146
147 // https://docs.python.org/2/c-api/init.html#c.PySys_SetArgvEx has important
147 // https://docs.python.org/2/c-api/init.html#c.PySys_SetArgvEx has important
148 // usage information about PySys_SetArgvEx:
148 // usage information about PySys_SetArgvEx:
149 //
149 //
150 // * It says the first argument should be the script that is being executed.
150 // * It says the first argument should be the script that is being executed.
151 // If not a script, it can be empty. We are definitely not a script.
151 // If not a script, it can be empty. We are definitely not a script.
152 // However, parts of Mercurial do look at sys.argv[0]. So we need to set
152 // However, parts of Mercurial do look at sys.argv[0]. So we need to set
153 // something here.
153 // something here.
154 //
154 //
155 // * When embedding Python, we should use ``PySys_SetArgvEx()`` and set
155 // * When embedding Python, we should use ``PySys_SetArgvEx()`` and set
156 // ``updatepath=0`` for security reasons. Essentially, Python's default
156 // ``updatepath=0`` for security reasons. Essentially, Python's default
157 // logic will treat an empty argv[0] in a manner that could result in
157 // logic will treat an empty argv[0] in a manner that could result in
158 // sys.path picking up directories it shouldn't and this could lead to
158 // sys.path picking up directories it shouldn't and this could lead to
159 // loading untrusted modules.
159 // loading untrusted modules.
160
160
161 // env::args() will panic if it sees a non-UTF-8 byte sequence. And
161 // env::args() will panic if it sees a non-UTF-8 byte sequence. And
162 // Mercurial supports arbitrary encodings of input data. So we need to
162 // Mercurial supports arbitrary encodings of input data. So we need to
163 // use OS-specific mechanisms to get the raw bytes without UTF-8
163 // use OS-specific mechanisms to get the raw bytes without UTF-8
164 // interference.
164 // interference.
165 let args = args_to_cstrings();
165 let args = args_to_cstrings();
166 let argv: Vec<*const c_char> = args.iter().map(|a| a.as_ptr()).collect();
166 let argv: Vec<*const c_char> = args.iter().map(|a| a.as_ptr()).collect();
167
167
168 unsafe {
168 unsafe {
169 python27_sys::PySys_SetArgvEx(args.len() as c_int, argv.as_ptr() as *mut *mut i8, 0);
169 python27_sys::PySys_SetArgvEx(args.len() as c_int, argv.as_ptr() as *mut *mut i8, 0);
170 }
170 }
171
171
172 let result;
172 let result;
173 {
173 {
174 // These need to be dropped before we call Py_Finalize(). Hence the
174 // These need to be dropped before we call Py_Finalize(). Hence the
175 // block.
175 // block.
176 let gil = Python::acquire_gil();
176 let gil = Python::acquire_gil();
177 let py = gil.python();
177 let py = gil.python();
178
178
179 // Mercurial code could call sys.exit(), which will call exit()
179 // Mercurial code could call sys.exit(), which will call exit()
180 // itself. So this may not return.
180 // itself. So this may not return.
181 // TODO this may cause issues on Windows due to the CRT mismatch.
181 // TODO this may cause issues on Windows due to the CRT mismatch.
182 // Investigate if we can intercept sys.exit() or SystemExit() to
182 // Investigate if we can intercept sys.exit() or SystemExit() to
183 // ensure we handle process exit.
183 // ensure we handle process exit.
184 result = match run_py(&env, py) {
184 result = match run_py(&env, py) {
185 // Print unhandled exceptions and exit code 255, as this is what
185 // Print unhandled exceptions and exit code 255, as this is what
186 // `python` does.
186 // `python` does.
187 Err(err) => {
187 Err(err) => {
188 err.print(py);
188 err.print(py);
189 Err(255)
189 Err(255)
190 }
190 }
191 Ok(()) => Ok(()),
191 Ok(()) => Ok(()),
192 };
192 };
193 }
193 }
194
194
195 unsafe {
195 unsafe {
196 python27_sys::Py_Finalize();
196 python27_sys::Py_Finalize();
197 }
197 }
198
198
199 result
199 result
200 }
200 }
201
201
202 fn run_py(env: &Environment, py: Python) -> PyResult<()> {
202 fn run_py(env: &Environment, py: Python) -> PyResult<()> {
203 let sys_mod = py.import("sys").unwrap();
203 let sys_mod = py.import("sys").unwrap();
204
204
205 update_encoding(py, &sys_mod);
205 update_encoding(py, &sys_mod);
206 update_modules_path(&env, py, &sys_mod);
206 update_modules_path(&env, py, &sys_mod);
207
207
208 // TODO consider a better error message on failure to import.
208 // TODO consider a better error message on failure to import.
209 let demand_mod = py.import("hgdemandimport")?;
209 let demand_mod = py.import("hgdemandimport")?;
210 demand_mod.call(py, "enable", NoArgs, None)?;
210 demand_mod.call(py, "enable", NoArgs, None)?;
211
211
212 let dispatch_mod = py.import("mercurial.dispatch")?;
212 let dispatch_mod = py.import("mercurial.dispatch")?;
213 dispatch_mod.call(py, "run", NoArgs, None)?;
213 dispatch_mod.call(py, "run", NoArgs, None)?;
214
214
215 Ok(())
215 Ok(())
216 }
216 }
217
217
218 fn main() {
218 fn main() {
219 let exit_code = match run() {
219 let exit_code = match run() {
220 Err(err) => err,
220 Err(err) => err,
221 Ok(()) => 0,
221 Ok(()) => 0,
222 };
222 };
223
223
224 std::process::exit(exit_code);
224 std::process::exit(exit_code);
225 }
225 }
General Comments 0
You need to be logged in to leave comments. Login now