Skip to content

Commit 3af9200

Browse files
committed
fix(setup): cannot create link in newer win11
err msg: OSError: [WinError 1314] 客户端没有所需的特权。: '..\\examples' -> 'fairseq\\examples'
1 parent 4480043 commit 3af9200

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

setup.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,27 @@ def get_files(path, relative_to="fairseq"):
243243
# symlink examples into fairseq package so package_data accepts them
244244
fairseq_examples = os.path.join("fairseq", "examples")
245245
if "build_ext" not in sys.argv[1:] and not os.path.exists(fairseq_examples):
246-
os.symlink(os.path.join("..", "examples"), fairseq_examples)
246+
if os.name == 'nt': # Windows
247+
# Use junction or copy instead of symlink on Windows
248+
import shutil
249+
250+
# Get absolute path to examples directory
251+
script_dir = os.path.dirname(os.path.abspath(__file__))
252+
examples_source = os.path.join(script_dir, "examples")
253+
254+
# Verify source exists
255+
if not os.path.exists(examples_source):
256+
raise FileNotFoundError(f"Source examples directory not found: {examples_source}")
257+
258+
try:
259+
# Try to create junction first (requires admin on older Windows)
260+
subprocess.run(['mklink', '/J', fairseq_examples, examples_source],
261+
shell=True, check=True)
262+
except (subprocess.CalledProcessError, FileNotFoundError):
263+
# Fallback to copying directory
264+
shutil.copytree(examples_source, fairseq_examples)
265+
else:
266+
os.symlink(os.path.join("..", "examples"), fairseq_examples)
247267

248268
package_data = {
249269
"fairseq": (
@@ -253,5 +273,20 @@ def get_files(path, relative_to="fairseq"):
253273
}
254274
do_setup(package_data)
255275
finally:
256-
if "build_ext" not in sys.argv[1:] and os.path.islink(fairseq_examples):
257-
os.unlink(fairseq_examples)
276+
if "build_ext" not in sys.argv[1:] and os.path.exists(fairseq_examples):
277+
if os.name == 'nt': # Windows
278+
import shutil
279+
try:
280+
# For junction points and directories, use rmtree
281+
if os.path.isdir(fairseq_examples):
282+
shutil.rmtree(fairseq_examples)
283+
except (OSError, PermissionError) as e:
284+
# If deletion fails, try alternative methods
285+
try:
286+
subprocess.run(['rmdir', '/S', '/Q', fairseq_examples],
287+
shell=True, check=True)
288+
except subprocess.CalledProcessError:
289+
print(f"Warning: Failed to remove {fairseq_examples}: {e}")
290+
else:
291+
if os.path.islink(fairseq_examples):
292+
os.unlink(fairseq_examples)

0 commit comments

Comments
 (0)